/**
 * meiguang8.com
 * Copyright (C) 2016-2019 All Rights Reserved.
 */
package com;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * class_name: Main
 * package: com
 * describe: TODO
 * @author: yuanyuan.zhu
 * creat_date: 2019/7/10
 * creat_time: 21:39
 **/
public class Main {
    public static void main(String[] args) {
        final JedisPool jedisPool = new
                JedisPool("47.98.195.93",6379);
        // 1000 个用户参与秒杀
        Jedis jedis = jedisPool.getResource();
        jedis.set("stock", "10");
        jedis.expire("stock", 60);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 90; i++) {
            executorService.execute(new Runnable() {
                public void run() {
                    Jedis resource = jedisPool.getResource();
                    Long stock = resource.decr("stock");

                    if (stock <= 0) {
                        System.out.println("跑完了。" );
                    }else {
                        System.out.println(stock + "Thread " + Thread.currentThread().getName());
                    }
                    resource.close();

                }
            });
        }

        executorService.shutdown();
    }
}
